// pages/order/order.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    latitude: '',
    longitude: ''
  },

  onTap: function() {
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  onTapGetTicket: function(e) {
    const concertId = e.currentTarget.id
    this.openid = app.globalData.openid
    console.log(this.openid)
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        var userLocation = this.data.latitude + ',' + this.data.longitude
        console.log(userLocation)
        wx.showLoading({
          title: '取票中...',
          mask: true
        })
        wx.cloud.callFunction({
          name: 'Get_Ticket',
          data: {
            userid: this.openid,
            concertid: concertId
          },
          success: res => {
            this.ticketdata = res.result.data[0]
            console.log(this.ticketdata)
            wx.cloud.callFunction({
              name: 'Within_Area',
              data: {
                from: userLocation,
                to: this.ticketdata.location,
                maxrange: "2000"
              },
              success: res => {
                console.log(res.result)
                if (res.result) {
                  const starttime = new Date(this.ticketdata.starttime).getTime();
                  const nowtime = new Date().getTime()
                  if (starttime - nowtime > 7200000)
                  {
                    wx.hideLoading()
                    wx.showToast({
                      title: '无法取票，请在节目开始前两个小时取票',
                      icon: 'none',
                      duration: 2000,
                      mask: true
                    })
                  }else{
                    wx.hideLoading()
                    wx.navigateTo({
                      url: '../verify_face/verify',
                    })
                  }
                } else {
                  wx.hideLoading()
                  wx.showToast({
                    title: '距离地点太远,无法取票',
                    icon: 'none',
                    duration: 1000,
                    mask: true
                  })
                }
              },
              fail: err => {
                wx.hideLoading()
                wx.showToast({
                  title: '取票失败',
                  icon: 'none',
                  duration: 1000,
                  mask: true
                })
              }
            })
          },
          fail: res => {
            wx.hideLoading()
            wx.showToast({
              title: '取票失败',
              icon: 'none',
              duration: 1000,
              mask: true
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.cloud.init()
    // this.concertid = options.concertid
    console.log("this.concertd:"+this.concertid)
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        this.openid = res.result.openid
        wx.cloud.callFunction({
          name: 'Get_Orders',
          data: {
            userid: this.openid
          },
          success: res => {
            console.log(res)
            this.setData({
              orderList: res.result
            })
          },
          fail: err => {

          }
        })
      },
      fail: err=>{
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})